package com.image.fresco.databinding;

import androidx.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.net.Uri;

import com.facebook.common.util.UriUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lib.util.ScreenUtil;
import com.image.fresco.FrescoUtil;

import java.io.File;

/**
 * @author qinbaowei
 * @createtime 2017/04/10 10:54
 * @email qbaowei@qq.com
 * @description
 */


public class DataBindingAdapter {

    @BindingAdapter("localSrc")
    public static void setLocalSrc(SimpleDraweeView simpleDraweeView, String path) {
        FrescoUtil.loadImage(simpleDraweeView, Uri.fromFile(new File(path)), 0, 0, null);
    }

    @BindingAdapter("src")
    public static void setSrc(SimpleDraweeView simpleDraweeView, String url) {
        FrescoUtil.loadImage(simpleDraweeView, url, 0, 0, null);
    }

    @BindingAdapter("resourceSrc")
    public static void setResourceSrc(SimpleDraweeView simpleDraweeView, int resId) {
        Uri uri = new Uri.Builder().scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
                                   .path(String.valueOf(resId))
                                   .build();
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                                                  .setUri(uri)
                                                  .setAutoPlayAnimations(true) // 设置加载图片完成后是否直接进行播放
                                                  .build();
        simpleDraweeView.setController(draweeController);
    }

    @BindingAdapter({"srcWR", "tWidth", "tRatio"})
    public static void setSrcWR(SimpleDraweeView simpleDraweeView,
                                String url,
                                int width,
                                float ratio) {
        FrescoUtil.loadImage(simpleDraweeView, url, width, (int) (width * ratio));
    }

    @BindingAdapter("srcReal")
    public static void setSrcReal(SimpleDraweeView simpleDraweeView, String url) {
        FrescoUtil.loadImage(simpleDraweeView, url, -1, -1);
    }

    @BindingAdapter("srcW28H10")
    public static void setSrcW28H10(SimpleDraweeView simpleDraweeView, String url) {
        int width = ScreenUtil.getScreenWidth(FrescoUtil.getContext());
        int height = (int) ((float) width / 2.8f);
        FrescoUtil.loadImage(simpleDraweeView, url, width, height, null);
    }

    @BindingAdapter("placeholder")
    public static void setPlaceholder(SimpleDraweeView simpleDraweeView, Drawable drawable) {
        GenericDraweeHierarchy hierarchy = simpleDraweeView.getHierarchy();
        hierarchy.setPlaceholderImage(drawable);
    }

    @BindingAdapter("placeholderRes")
    public static void setPlaceholderRes(SimpleDraweeView simpleDraweeView, int drawableRes) {
        GenericDraweeHierarchy hierarchy = simpleDraweeView.getHierarchy();
        hierarchy.setPlaceholderImage(drawableRes);
    }

    @BindingAdapter("sdvRatio")
    public static void setSdvRatio(SimpleDraweeView simpleDraweeView, float ratio) {
        simpleDraweeView.setAspectRatio(ratio);
    }
}
